package com.AX.controller;

import com.AX.DTO.ParkingLotInfo;
import com.AX.DTO.ParkingSpaceStatus;
import com.AX.entity.ParkingLot;
import com.AX.service.impl.ParkingLotService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/parking-lots")
@RequiredArgsConstructor
public class ParkingLotController {
    private final ParkingLotService parkingLotService;

    // 功能一：获取所有停车场基本信息
    @GetMapping("/lots")
    public ResponseEntity<List<ParkingLotInfo>> getAllParkingLots() {
        System.out.println("返回成功");
        return ResponseEntity.ok(parkingLotService.getAllParkingLotsInfo());
    }



    // 功能二：获取指定停车场车位状态
    @GetMapping("/{parkingLotId}/spaces")
    public ResponseEntity<List<ParkingSpaceStatus>> getParkingSpaces(@PathVariable Integer parkingLotId) {
        System.out.println("返回成功1");
        List<ParkingSpaceStatus> tmp = parkingLotService.getParkingSpaceStatus(parkingLotId);
        for (ParkingSpaceStatus num : tmp) {
            System.out.println("元素: " + num);
        }

        return ResponseEntity.ok(tmp);
    }

    // 功能三：获取指定停车场信息
    @GetMapping("/{parkingLotId}")
    public ResponseEntity<ParkingLot> getParkingLot(@PathVariable Integer parkingLotId) {
        System.out.println("获取停车场详情: " + parkingLotId);
        return ResponseEntity.ok(parkingLotService.getParkingLotStatus(parkingLotId));
    }


    // 功能四：更新车位状态
    @PutMapping("/{parkingLotId}/spaces/{spaceId}/occupy")
    public ResponseEntity<?> occupyParkingSpace(@PathVariable Integer parkingLotId, @PathVariable String spaceId) {
        try {
            parkingLotService.updateSpaceStatus(parkingLotId, spaceId, "occupied");
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "状态更新失败"));
        }
    }

    // 功能五：插入停车记录
    @PutMapping("/{userId}/spaces/{spaceId}/incord")
    public ResponseEntity<?> insertCords(@PathVariable Integer userId, @PathVariable String spaceId) {
        try {
            System.out.println("用户id为：");
            System.out.println(userId);
            System.out.println("目标点为：");
            System.out.println(spaceId);
            parkingLotService.insertParkingRecord(spaceId,userId);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            System.out.println("出错");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "状态更新失败"));
        }
    }



    // // 功能六：检查车位是否属于用户
    @GetMapping("/{parkingLotId}/spaces/{spaceId}/check-ownership")
    public ResponseEntity<Boolean> checkSpaceOwnership(@PathVariable Integer parkingLotId, @PathVariable String spaceId, @RequestHeader("userId") Integer userId) {
        boolean isOwner = parkingLotService.checkSpaceOwnership(spaceId, userId);
        return ResponseEntity.ok(isOwner);
    }

     // 功能七：释放车位
    @PutMapping("/{parkingLotId}/spaces/{spaceId}/release")
    public ResponseEntity<?> releaseParkingSpace(
            @PathVariable Integer parkingLotId,
            @PathVariable String spaceId
    ) {
        try {
            parkingLotService.updateSpaceStatus(parkingLotId, spaceId, "available");
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "车位释放失败"));
        }
    }

    // 功能八：删除停车记录
    @DeleteMapping("/{userId}/spaces/{spaceId}/record")
    public ResponseEntity<?> deleteParkingRecord(
            @PathVariable Integer userId,
            @PathVariable String spaceId
    ) {
        try {
            parkingLotService.deleteParkingRecord(spaceId, userId);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("error", "记录删除失败"));
        }
    }
}
